package com.huluobo.a2207douyindemo.mine;

import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.adapter.LikeCollectAdapter;
import com.huluobo.a2207douyindemo.databinding.FragmentLikeBinding;
import com.huluobo.base.mvvm.BaseMVVMFragment;

public class LikeFragment extends BaseMVVMFragment<FragmentLikeBinding, MineViewModel> {

    private LikeCollectAdapter likeCollectAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_like;
    }

    @Override
    protected void initData() {
        vm.getLikeVideos();

        vm.getLikeVideosSuccess.observe(this, videoBeanResult -> {
            if (videoBeanResult.getCode() == 200) {
                likeCollectAdapter.getData().clear();
                likeCollectAdapter.getData().addAll(videoBeanResult.getData());
                likeCollectAdapter.notifyDataSetChanged();
            } else {
                ToastUtils.showLong(videoBeanResult.getMessage());
            }
        });

        vm.getLikeVideosFailed.observe(this, s -> {
            Log.i("TagA", "获取点赞视频失败:" + s);
        });

        vm.noLikeVideoSuccess.observe(this, commonBeanResult -> {
            ToastUtils.showLong(commonBeanResult.getMessage());
            if (commonBeanResult.getCode() == 200) {
                vm.getLikeVideos();
            }
        });

        vm.noLikeVideoFailed.observe(this, s -> {
            Log.i("TagA", "取消点赞失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.likeRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        likeCollectAdapter = new LikeCollectAdapter();
        binding.likeRv.setAdapter(likeCollectAdapter);

        likeCollectAdapter.addChildClickViewIds(R.id.like_collect_delete_btn);
        likeCollectAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int videoId = likeCollectAdapter.getData().get(position).getId();
            vm.noLikeVideo(videoId);
        });
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}